package cn.org.seventh.tools.plugin;

import org.generama.FreeMarkerTemplateEngine;
import org.generama.JellyTemplateEngine;
import org.generama.QDoxCapableMetadataProvider;
import org.generama.WriterMapper;
import org.generama.defaults.JavaGeneratingPlugin;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

public class ServiceMethodInterfacePlugin extends JavaGeneratingPlugin {

	/**
	 * 要替换的类名后缀
	 */
	private String suffix = "Manage";

	/**
	 * 替换后的类名后缀
	 */
	private String suffixTo = "Service";

	public String getSuffix() {
		return this.suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getSuffixTo() {
		return this.suffixTo;
	}

	public void setSuffixTo(String suffixTo) {
		this.suffixTo = suffixTo;
	}

	public ServiceMethodInterfacePlugin(JellyTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(true);
		setFileregex(suffix + "\\.java");
		setFilereplace(suffixTo + ".java");
		new TagLibrary(metadataProvider);
	}

	public ServiceMethodInterfacePlugin(
			FreeMarkerTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(true);
		setFileregex(suffix + "\\.java");
		setFilereplace(suffixTo + ".java");
		new TagLibrary(metadataProvider);
	}

	public boolean isAllowedMethod(Object metadata) {
		JavaMethod method = (JavaMethod) metadata;
		if (method.isNative() || method.isFinal() || method.isStatic()
				|| method.isPrivate()) {
			return false;
		}
		return true;
	}

	public boolean shouldGenerate(Object metadata) {
		JavaClass javaClass = (JavaClass) metadata;
		return javaClass.getTagByName(SpringManageTagImpl.NAME) == null ? false
				: true;
	}
}